// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');

var userName = Titanium.App.Properties.getString("username");
if(!userName){
	var dbase = Titanium.Database.open('gyaan');
	dbase.execute('DROP TABLE IF EXISTS questions');
	dbase.execute('CREATE TABLE IF NOT EXISTS questions (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, actual_answer TEXT, type TEXT, lesson_id INTEGER, answer_provided TEXT, q_id TEXT)');
	dbase.execute('CREATE TABLE IF NOT EXISTS options (id INTEGER PRIMARY KEY AUTOINCREMENT, q_id INTEGER, a TEXT, b TEXT, c TEXT, d TEXT, e TEXT)');
	dbase.execute('CREATE TABLE IF NOT EXISTS q_stats (id INTEGER PRIMARY KEY, q_id INTEGER, ratings TEXT, user_id INTEGER, share_count INTEGER)');
	dbase.execute('CREATE TABLE IF NOT EXISTS user_questions (id INTEGER PRIMARY KEY, q_id INTEGER, student_email TEXT)');
	dbase.execute('CREATE TABLE IF NOT EXISTS user_answers (id INTEGER PRIMARY KEY, q_id INTEGER, answer TEXT)');
	
	var maxRow = 0;
	dbase.execute('DELETE FROM questions');
	dbase.execute('DELETE FROM options');
	var myResultSet = dbase.execute('SELECT * FROM questions');
	if (!myResultSet.isValidRow()) {
	// We have an empty database ...
	// Add data to the database table
	dbase.execute('INSERT INTO questions (id, title, actual_answer, type, lesson_id, q_id ) VALUES(?, ?, ?, ?, ?, ?)', 1,  'If n is an even integer and a multiple of 3, which of the following cannot be an even integer?:', 'E','Number Properties',1, '1');
	dbase.execute('INSERT INTO questions (id, title, actual_answer, type, lesson_id, q_id) VALUES(?, ?, ?, ?, ?,?)', 2,  'The price of a bag of peanuts is $5.50. The price of 3 bags of peanuts, plus 2 bags of popcorn is $19.00. What is the price of a bag of popcorn?:', 'B','Algebra',1,'2');
	dbase.execute('INSERT INTO questions (id, title, actual_answer, type, lesson_id, q_id) VALUES(?, ?, ?, ?, ?,?)', 3, 'There are 84 supermarkets in the FGH chain. All of them are either in the US or Canada. If there are 22 more FGH supermarkets in the US than in Canada, how many FGH supermarkets are there in the US?', 'D','Algebra',1,'3');
	
	dbase.execute('INSERT INTO options (id, q_id, a, b, c, d, e) VALUES(?, ?, ?, ?, ?, ?, ?)', 1,  1, 'n/6.','n/3.','3n.', '3n + 2.','3n + 3.');
	dbase.execute('INSERT INTO options (id, q_id, a, b, c, d, e) VALUES(?, ?, ?, ?, ?, ?, ?)', 2,  2, '$1.00.','$1.25.','$1.50.', '$1.75.', '$2.00.');
	dbase.execute('INSERT INTO options (id, q_id, a, b, c, d, e) VALUES(?, ?, ?, ?, ?, ?, ?)', 3, 3, '20.','31.','42.', '53.', '64.');
	
	}
	myResultSet.close();
	dbase.close();
	var win_login=Titanium.UI.createWindow({
    url:'tabs/registration.js',
    title:'Registration',
        navBarHidden:true
});
win_login.open();
}else{

// create tab group
var tabGroup = Titanium.UI.createTabGroup();


//
// create base UI tab and root window
//
var win1 = Titanium.UI.createWindow({  
    title:'Dashboard',
    url:'tabs/dashboard.js',
    backgroundColor:'#fff'
});
var tab1 = Titanium.UI.createTab({  
    icon:'home.png',
    title:'Dashboard',
    window:win1
});

var win2 = Titanium.UI.createWindow({
	url:'tabs/workflow.js',
	backgroundColor:'#fff',
	title:'Workflow'
});
var tab2 = Titanium.UI.createTab({
	icon:'lesson.png',
	title:'Workflow',
	window:win2
});

var win3 = Titanium.UI.createWindow({  
    title:'Assessment',
    url:'tabs/assessment.js',
    backgroundColor:'#fff'
});
var tab3 = Titanium.UI.createTab({  
    icon:'video.png',
    title:'Assessment',
    window:win3
});

//
// create controls tab and root window
//
var win4 = Titanium.UI.createWindow({
	url:'tabs/testTemplate.js',
	backgroundColor:'#fff',
	title:'Practice'
});
var tab4 = Titanium.UI.createTab({
	icon:'test.png',
	title:'Practice',
	window:win4
});
tabGroup.addTab(tab1);  
tabGroup.addTab(tab2);  
tabGroup.addTab(tab3);
tabGroup.addTab(tab4);


if(Titanium.App.Properties.getString("userType")!="" &&  Titanium.App.Properties.getString("userType")!=null
&& Titanium.App.Properties.getString("userType").toUpperCase()=="TEACHER"){
	var win5 = Titanium.UI.createWindow({  
	    title:'Add Questions',
	    url:'tabs/qBank.js',
	    backgroundColor:'#fff'
	});
	var tab5 = Titanium.UI.createTab({  
	    icon:'resources.png',
	    title:'Questions',
	    window:win5
	});
	tabGroup.addTab(tab5); 
	var win6 = Titanium.UI.createWindow({  
	    title:'Record',
	    url:'tabs/resource.js',
	    backgroundColor:'#fff'
	});
	var tab6 = Titanium.UI.createTab({  
	    icon:'resources.png',
	    title:'Record',
	    window:win6
	});
	tabGroup.addTab(tab6); 
}

// open tab group
tabGroup.open();
}